#Region " Imports "

Imports BlazeApps.Library.Editor
Imports BlazeApps.Library.Page
Imports BlazeApps.Library.WorkFlow

#End Region

Partial Class Usercontrols_Editor
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected PageGuid As Guid
    Protected TemplateGuid As Guid

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Page Editor")

        ConfigBaseEditorCtrls()
        GetQueryStringValues()
        GetEditorDetail(PageGuid)
		'BuildEditor(EditorPageType.DynamicRepeaterEditors)
		BuildFCKEditor(EditorPageType.DynamicRepeaterEditors)

        Me.uxEditorPanel.Visible = ShowControls(SecurityMask.Editor, True)

    End Sub

#End Region

#Region " Save Data "

    Private Function SaveEditorData() As Boolean
        Dim PageItems As PageDetail = PageDetail.GetPageDetail(PageGuid)
        Dim iI As Integer

        Try
            For iI = 1 To Me.uxEditorRepeater.Items.Count
                Dim Content As PageWorkingContent = PageWorkingContent.NewPageWorkingContent
                Dim ctl As Control = Me.uxEditorRepeater.Items(iI - 1).FindControl("uxTemplate")
				'Dim Editor As FreeTextBox = ctl.Controls.Item(0).FindControl("uxEditorFreeTextBox")
				Dim FCK As FredCK.FCKeditorV2.FCKeditor = ctl.Controls.Item(0).FindControl("uxEditorTextbox")

                Content.pageid = PageGuid
                Content.PlaceholderNumber = iI
				Content.PlaceholderData = FCK.Value
                Content.pageRevision = PageItems.revision
                Content.DBConnection = ConnectionString
                Content.LastUserid = Membership.GetUser(CurrentUser).ProviderUserKey
                Content.Save()
            Next

            Return True

        Catch ex As Exception

        End Try

    End Function

#End Region

#Region " Load Data "

    Private Function GetEditorDetail(ByVal PageID As Guid) As Boolean

        Dim List As New EditorList

        With Me.uxEditorRepeater
            .DataSource = List.GetEditorList(PageID)
            .DataBind()
        End With

    End Function

#End Region

#Region " Form Controls Events "

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        SaveEditorData()
    End Sub

    Protected Sub uxPreviewButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxPreviewButton.Click
        SaveEditorData()
        'Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "preview", "fPopUpWin('" & String.Format("{0}.aspx?pid=viewer&popup=1&preview=1&id={1}", AppPage, PageGuid.ToString) & "','preview',0 , 1, 1,800,600, 0);", True)
        ScriptManager.RegisterStartupScript(uxPreviewButton, Me.GetType(), "preview", "fPopUpWin('" & String.Format("{0}.aspx?pid=viewer&popup=1&preview=1&id={1}", AppPage, PageGuid.ToString) & "','preview',0 , 1, 1,800,600, 0);", True)
    End Sub

    Protected Sub uxExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxExitButton.Click
        Response.Redirect(String.Format("{0}.aspx?pid=management&id={1}", AppPage, PageGuid.ToString))
    End Sub

    Protected Sub uxSaveExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveExitButton.Click
        SaveEditorData()
        Response.Redirect(String.Format("{0}.aspx?pid=management&id={1}", AppPage, PageGuid.ToString))
    End Sub

    Protected Sub uxResourcesButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResourcesButton.Click
        'Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "resourcemgr", "fPopUpWin('" & String.Format("{0}.aspx?pid=res_resourcegallery&popup=1", AppPage) & "','resourcemgr',0 , 1, 1,790,500, 0);", True)
        ScriptManager.RegisterStartupScript(uxResourcesButton, Me.GetType(), "resourcemgr", "fPopUpWin('" & String.Format("{0}.aspx?pid=res_resourcegallery&popup=1", AppPage) & "','resourcemgr',0 , 1, 1,790,500, 0);", True)
    End Sub

    Protected Sub uxPublishButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxPublishButton.Click
        Dim Published As New PublishPage

        SaveEditorData()
        Published.PushWorkingDataToLive(PageGuid)
		Response.Redirect(String.Format("{0}.aspx?pid=viewer&id={1}", AppPage, PageGuid.ToString))
    End Sub

    Protected Sub uxEditorRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles uxEditorRepeater.ItemDataBound
        Dim ctl As Control = e.Item.FindControl("uxTemplate")
        Dim lit As Literal = ctl.Controls.Item(0).FindControl("uxTitleLiteral")
		'Dim ftb As FreeTextBox = ctl.Controls.Item(0).FindControl("uxEditorFreeTextBox")
		Dim FCK As FredCK.FCKeditorV2.FCKeditor = ctl.Controls.Item(0).FindControl("uxEditorTextbox")

        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            If ShowItem(ConvertToGuid(e.Item.DataItem("templatefieldid").ToString)) = True Then
                lit.Text = e.Item.DataItem("fieldname").ToString

                'ftb.Width = e.Item.DataItem("fieldwidth").ToString
				FCK.Height = New Unit((Convert.ToInt32(e.Item.DataItem("fieldheight").ToString) + 75), UnitType.Pixel)
				FCK.Value = e.Item.DataItem("placeholderdata").ToString
            Else
                e.Item.Visible = False
            End If
        End If

    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseEditorCtrls()
		'Me.FTBRepeater = Me.uxEditorRepeater
		Me.FCKRepeater = Me.uxEditorRepeater
    End Sub

    Private Sub GetQueryStringValues()
        PageGuid = GetGuidFromQueryString("id")
        TemplateGuid = GetGuidFromQueryString("tid")
    End Sub

#End Region

End Class
